﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulldogCMS.Core.MVC;
using System.IO;

namespace BulldogCMS.Core.Runtime.Handlers
{
    public class ResourceHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string extension = Path.GetExtension(context.Request.AppRelativeCurrentExecutionFilePath);

            context.Response.ContentType = this.GetContentTypeForExtension(extension);

            ResourceFilesHelper helper = new ResourceFilesHelper();
            string path = context.Request.AppRelativeCurrentExecutionFilePath;
            helper.GetResourceStream(path).CopyTo(context.Response.OutputStream);
            context.Response.Flush();               
        }

        public string GetContentTypeForExtension(string ext)
        {
            switch (ext)
            {
                case ".css": return "text/css";
                case ".js": return "application/javascript";
                case ".png": return "image/png";
                case ".jpg":
                case ".jpeg": return "image/jpeg";
            }
            return "application/octet-stream";
        }

    }
}